package com.sky.mapper;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author ghy
 * @version 1.0.1
 * @date 2023-04-28 10:13:30
 */
@Mapper
public interface OrderMapper {

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("    insert into orders ( number, status, user_id, address_book_id, order_time, checkout_time, pay_method, " +
            "                         pay_status, amount, remark, phone, address, user_name, consignee, " +
            "                         cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, " +
            "                         delivery_status, delivery_time, pack_amount, tableware_number, tableware_status)" +
            "    values ( #{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime}," +
            "        #{payMethod},#{payStatus},#{amount},#{remark},#{phone},#{address},#{userName},#{consignee}," +
            "        #{cancelReason},#{rejectionReason},#{cancelTime},#{estimatedDeliveryTime},#{deliveryStatus}," +
            "        #{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})")
    void insert(Orders orders);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    @Select("select * from orders where order_time < #{orderTime} and status = #{status}")
    List<Orders> getByOrderTimeAndStatus(@Param("orderTime") LocalDateTime orderTime, @Param("status") Integer status);

    @Select("select * from orders where id = #{id}")
    Orders getId(Long id);

    BigDecimal getDayTurnover(@Param("minTime") LocalDateTime minTime,
                              @Param("maxTime") LocalDateTime maxTime,
                              @Param("status") Integer status);

    Long getDayCount(@Param("minTime") LocalDateTime minTime,
                     @Param("maxTime") LocalDateTime maxTime,
                     @Param("status")  Integer status);

    List<GoodsSalesDTO> getSaleTop10(@Param("minTime") LocalDateTime minTime,
                                     @Param("maxTime") LocalDateTime maxTime,
                                     @Param("status") Integer status);

    /**
     * 根据动态条件统计营业额数据
     * @param map
     * @return
     */
    Double sumByMap(Map map);

    /**
     * 根据动态条件统计订单数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}


